package com.zsc.wxapp.services.impl;

import com.zsc.wxapp.entity.CollectedMonthAndSeason;
import com.zsc.wxapp.entity.User;
import com.zsc.wxapp.entity.vo.CustomerVO;
import com.zsc.wxapp.entity.vo.RuntimeDataVO;
import com.zsc.wxapp.exservices.Remote2;
import com.zsc.wxapp.mapper.CollectedDateMapper;
import com.zsc.wxapp.mapper.CollectedMonthAndSeasonMapper;
import com.zsc.wxapp.mapper.UserMapper;
import com.zsc.wxapp.services.StatisticsDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class StatisticsDataServiceImpl implements StatisticsDataService {

    @Resource
    private CollectedDateMapper collectedDateMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CollectedMonthAndSeasonMapper collectedMonthAndSeasonMapper;
    @Resource
    private Remote2 remote2;

    /**
     * 获取当天、本季度、当年运行数据
     * @return RuntimeData
     */
    @Override
    public RuntimeDataVO getRuntimeData() {
        //获取上传数量的数据
        Integer custNum = userMapper.getCustomerNum(0);
        Integer uploadNum = userMapper.getCustomerNum(2);
        BigDecimal uploadRatioDay;
        try {
            uploadRatioDay = BigDecimal.valueOf(uploadNum).divide(BigDecimal.valueOf(custNum));
        } catch (ArithmeticException e) {
            log.error("系统出错！");
            return null;
        }

        List<User> users = userMapper.getAllEndUser(null);

        // 计算季度数据
        // 获取当天属于哪个季度
        LocalDate currentDate = LocalDate.now();
        Integer quarter = (currentDate.getMonthValue() - 1) / 3 + 1;
        Integer season = 0;
        Integer year = 0;
        for (User user : users) {
            //判断当前用户在本季度有没有上传价签数据
            if (user.getUploadDate() != null && ((user.getUploadDate().getMonthValue() - 1) / 3 + 1) == quarter) {
                season++;
            }
            //判断年度有没有上传价签数据
            if (user.getUploadDate() != null && user.getUploadDate().getYear() == currentDate.getYear()) {
                year++;
            }
        }
        BigDecimal uploadRatioSeason = BigDecimal.valueOf(season).divide(BigDecimal.valueOf(custNum));

        // 计算年度数据
        BigDecimal uploadRatioYear = BigDecimal.valueOf(year).divide(BigDecimal.valueOf(custNum));


        RuntimeDataVO result = RuntimeDataVO.builder()
                .uploadNum(uploadNum)
                .uploadRatioDay(uploadRatioDay)
                .uploadRatioSeason(uploadRatioSeason)
                .uploadRatioYear(uploadRatioYear)
                .build();
        return result;
    }

    /**
     * 按时间类型获取用户上传数据
     * @param type 时间类型
     * @return
     */
    @Override
    public RuntimeDataVO getRunTimeDataByTime(Integer type) {
        List<Integer> list = new ArrayList<>();

        if (type == 1) {
            //按天数返回
            list = collectedDateMapper.getDataByTime();
        } else {
            //按月或者季度返回
            List<CollectedMonthAndSeason> monthAndSeasons = collectedMonthAndSeasonMapper.getDataByTime(type);
            if (type == 2) {
                //按月数据返回
                for (CollectedMonthAndSeason monthAndSeason : monthAndSeasons) {
                    list.add(monthAndSeason.getCollectedMonthNum());
                }
            } else {
                //按季度数据返回
                for (CollectedMonthAndSeason monthAndSeason : monthAndSeasons) {
                    list.add(monthAndSeason.getCollectedSeasonNum());
                }
            }
        }

        return RuntimeDataVO.builder()
                .recentDataList(list)
                .build();
    }

    /**
     * 按地区获取运行数据
     * @return
     */
    @Override
    public RuntimeDataVO getRuntimeDataByArea() {
        List<User> users = userMapper.getAllEndUser(null);
        LocalDate currentDate = LocalDate.now();
        Map<String, Integer> result = new HashMap<>();
        result.put("火炬开发区", 0);
        result.put("东区", 0);
        result.put("三乡镇", 0);
        result.put("小榄镇", 0);
        result.put("沙溪镇", 0);
        result.put("西区", 0);
        result.put("黄圃镇", 0);
        result.put("石岐区", 0);

        for (User user : users) {
            if (user.getUploadDate().isEqual(currentDate)) {
                result.put(user.getArea(), result.getOrDefault(user.getArea(), 0) + 1);
            }
        }

        return RuntimeDataVO.builder()
                .areaDataList(result)
                .build();
    }

    /**
     * 按用户类型获取运行数据
     * @return RuntimeDataVO
     */
    @Override
    public RuntimeDataVO getRuntimeDataByCustType(Integer type) {
        List<User> users = userMapper.getAllEndUser(null);
        List<CustomerVO> list = new ArrayList<>();
        Map<String, Integer> result = new HashMap<>();

        //调用远程服务获取用户数据
        for (User user : users) {
            CustomerVO userInfo = remote2.getCustomerInfoByCustUuid(user.getCustomUuid());
            list.add(userInfo);
        }

        if (type == 1) {
            //按挡位统计
            for (CustomerVO user : list) {
                result.put(user.getCustTypeName(), result.getOrDefault(user.getCustTypeName(), 0) + 1);
            }
        } else if (type == 2) {
            for (CustomerVO user : list) {
                result.put(user.getBusiPlace(), result.getOrDefault(user.getBusiPlace(), 0) + 1);
            }
        } else {
            for (CustomerVO user : list) {
                result.put(user.getTerminalLevel(), result.getOrDefault(user.getTerminalLevel(), 0) + 1);
            }
        }

        return RuntimeDataVO.builder()
                .custTypeDataList(result)
                .build();
    }
}
